package com.ruoyi.project.user.host.controller;

import com.ruoyi.common.constant.SqlOrder;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.user.host.domain.Host;
import com.ruoyi.project.user.host.service.HostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/user/host")
public class HostController extends BaseController {

    private String prefix = "user/host";

    @Autowired
    private HostService hostService;

    @GetMapping()
    @RequiresPermissions("user:host:view")
    public String host(){

        return prefix + "/host";
    }

    /**
     * 页面数据展示
     * @return
     */
    @RequiresPermissions("user:host:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Host host,int hostOrderNum){
        System.out.println(hostOrderNum);
        mystartPage(SqlOrder.getSqlOrder(hostOrderNum));
        List<Host>  list = hostService.selectHost(host);
        return getDataTable(list);

    }


    @GetMapping("/add")
    @RequiresPermissions("user:host:add")
    public String hostAdd(){
        return  prefix +"/add";
    }


    /**
     * 查询手机号
     * @param hostPhone
     * @return
     */
    @RequiresPermissions("user:host:lsit")
    @PostMapping("/checkHostPhone")
    @ResponseBody
    public Long checkHostPhone(String hostPhone){

        return hostService.selectByPhone(hostPhone);
    }

    /**
     * 查询邮箱是否存在
     * @param hostEmail
     * @return
     */
    @RequiresPermissions("user:host:lsit")
    @PostMapping("/checkHostEmail")
    @ResponseBody
    public Long checkHostEmail(String hostEmail){

        return hostService.selectByEmail(hostEmail);
    }

    /**
     * 添加主持人
     * @param host
     * @param hostPassword
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    @RequiresPermissions("user:host:add")
    public AjaxResult add(Host host,String hostPassword){


        return toAjax(hostService.insertHost(host,hostPassword));
    }

    /**
     * 权重修改
     * @param host
     * @return
     */
    @ResponseBody
    @PostMapping("/update/sortWeight")
    @RequiresPermissions("user:host:sortWeight")
    public AjaxResult updateHostSortWeight(Host host){
        return toAjax(hostService.updateHostSortWeight(host));
    }



    @Log(title = "主持人管理",businessType = BusinessType.EXPORT)
    @RequiresPermissions("user:host:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Host host,int hostOrderNum){

        List<Host> list = hostService.exportSort(host, hostOrderNum);
        ExcelUtil<Host> excelUtil = new ExcelUtil<Host>(Host.class);

        return excelUtil.exportExcel(list,"主持人管理");

    }


    /**
     * 批量删除主持人
     * @param ids
     * @return
     */
    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("user:host:delete")
    public AjaxResult deleteHost(Long [] ids){
        return toAjax(hostService.deleteHostByIds(ids));
    }



    @RequiresPermissions("user:host:update")
    @GetMapping("/edit/{hostId}")
    public String selectEdit(@PathVariable("hostId") Long hostId, Model model){

        model.addAttribute("host",hostService.selectHostById(hostId));
        return prefix +"/edit";

    }



}
